package com.zret.oasys.dao.impl;

import java.util.List;

import com.zret.oasys.dao.DeptDao;
import com.zret.oasys.dbtools.JDBCUtils;
import com.zret.oasys.dbtools.PageData;
import com.zret.oasys.entity.Dept;

public class DeptDaoImpl implements DeptDao 
{
	//增加部门
	@Override
	public boolean addDepts(Dept dept) 
	{
		String sql = "insert into dept values(?,?,?)";
		int n = JDBCUtils.exUpdate(sql, dept.getDeptid(),dept.getManagerid(),dept.getDeptname());
		return n>0;
	}
	
	//删除部门
	@Override
	public boolean deleteDepts(int deptid) 
	{
		String sql = "delete from dept where deptid = ?";
		return JDBCUtils.exUpdate(sql, deptid)>0;
	}
	
	//修改员工
	@Override
	public boolean updateDept(Dept dept)
	{
		String sql = "update dept set managerid=?,deptname=? where deptid=?";
		int n = JDBCUtils.exUpdate(sql, dept.getManagerid(),dept.getDeptname(),dept.getDeptid());
		return n >0;
	}
	
	//查询部门
	@Override
	public List<Dept> getDepts() 
	{
		String sql = "select * from dept";
		List<Dept> list = JDBCUtils.exQuery(sql, Dept.class, null);
		return list;
	}
	
	//查询部门通过部门ID
	@Override
	public List<Dept> getDeptByDeptId(int deptid) 
	{
		String sql = "select * from dept where deptid=?";
		List<Dept> list = JDBCUtils.exQuery(sql, Dept.class, deptid);
		return list;
	}
	
	//模糊查询部门通过部门名称
	@Override
	public List<Dept> getDeptByDeptName(String deptname)
	{
		String sql = "select * from dept where deptname like ?";
		List<Dept> list = JDBCUtils.exQuery(sql, Dept.class, deptname);
		return list;
	}
	
	//查询部门通过部门经理ID
	@Override
	public List<Dept> getDeptByManagerid(int managerid) 
	{
		String sql = "select * from dept where managerid=?";
		List<Dept> list = JDBCUtils.exQuery(sql, Dept.class, managerid);
		return list;
	}
	
	//分页查询部门
	@Override
	public PageData<Dept> getDeptsByPage(int page, int pageSize) 
	{
		String sql = "select * from dept";
		PageData<Dept> pd  =JDBCUtils.exQueryByPage(sql, Dept.class, page, pageSize, null);
		return pd;
	}
	
	//分页模糊查询通过部门名称
	@Override
	public PageData<Dept> getDeptsByPageForDeptname(int page, int pageSize, String deptname)
	{
		String sql = "select * from dept where deptname like ?";
		PageData<Dept> pd = JDBCUtils.exQueryByPage(sql, Dept.class, page, pageSize, "%"+deptname+"%");
		return pd;
	}
}
